package com.xuebusi.toutiao.admin.api.article.controller;

import com.xuebusi.toutiao.admin.api.article.service.ArticleChannelService;
import com.xuebusi.toutiao.admin.api.article.service.ArticleService;
import com.xuebusi.toutiao.admin.api.article.vo.*;
import com.xuebusi.toutiao.admin.api.common.annotation.LoginUser;
import com.xuebusi.toutiao.admin.api.common.result.ApiResult;
import com.xuebusi.toutiao.admin.api.common.result.PageInfo;
import com.xuebusi.toutiao.admin.api.user.entity.UserEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 文章控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/mp/v1_0")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleChannelService articleChannelService;

    /**
     * 分页查询文章列表
     *
     * @param queryPVO 查询条件
     * @return
     */
    @GetMapping("/articles")
    public ResponseEntity<ApiResult<PageInfo<ArticlePageRVO>>> pageList(ArticleQueryPVO queryPVO) {
        return articleService.pageList(queryPVO);
    }

    /**
     * 创建文章
     *
     * @param loginUser 登录用户
     * @param draft     是否草稿
     * @param createPVO 文章信息
     * @return
     */
    @PostMapping("/articles")
    public ResponseEntity<ApiResult<ArticleCreateRVO>> createArticle(
            @LoginUser UserEntity loginUser,
            @RequestParam("draft") Boolean draft,
            @RequestBody @Valid ArticleCreatePVO createPVO) {
        return articleService.createArticle(loginUser.getId(), draft, createPVO);
    }

    /**
     * 修改文章
     *
     * @param loginUser 登录用户
     * @param articleId 文章id
     * @param draft     是否草稿
     * @param updatePVO 更新字段
     * @return
     */
    @PutMapping("/articles/{target}")
    public ResponseEntity<ApiResult<ArticleCreateRVO>> updateArticle(
            @LoginUser UserEntity loginUser,
            @PathVariable("target") Integer articleId,
            @RequestParam("draft") Boolean draft,
            @RequestBody ArticleUpdatePVO updatePVO) {
        return articleService.updateArticle(loginUser.getId(), articleId, draft, updatePVO);
    }

    /**
     * 获取文章详情
     *
     * @param articleId 文章id
     * @return
     */
    @GetMapping("/articles/{target}")
    public ResponseEntity<ApiResult<ArticleDetailVO>> getArticle(@PathVariable("target") Integer articleId) {
        return articleService.getArticle(articleId);
    }

    /**
     * 删除文章
     *
     * @param loginUser 登录用户
     * @param articleId 文章id
     * @return
     */
    @DeleteMapping("/articles/{target}")
    public ResponseEntity delete(@LoginUser UserEntity loginUser, @PathVariable("target") Integer articleId) {
        return articleService.delete(loginUser.getId(), articleId);
    }

    /**
     * 搜索文章
     *
     * @param searchPVO 搜索参数
     * @return
     */
    @GetMapping("/search")
    public ResponseEntity<ApiResult<PageInfo<ArticleSearchRVO>>> search(ArticleSearchPVO searchPVO) {
        return articleService.search(searchPVO);
    }

    /**
     * 文章频道列表
     *
     * @return
     */
    @GetMapping("/channels")
    public ResponseEntity<ApiResult<ChannelRVO>> getChannels() {
        return articleChannelService.getChannels();
    }
}
